package taskGenerator;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Timer;

import model.SmartTaskUtility;
import model.taskQueueUtility;
import util.IniParameters;

public class TaskGenerator {

	private final int m_1stDelay = 1000;

	private SmartTaskUtility m_leftTasks;
	private taskQueueUtility m_rightTaskQueueUtility;
	private Behavior m_behavior = new PureRandom();
	private Timer m_timer;

	public TaskGenerator(SmartTaskUtility leftTasks,
			taskQueueUtility rightTaskQueueUtility) {
		m_leftTasks = leftTasks;
		m_rightTaskQueueUtility = rightTaskQueueUtility;

		m_timer = new Timer(m_1stDelay, timerListener);
		m_timer.setDelay(IniParameters.userComingIntervalDefault);
	}

	public void startGenerating() {
		if (!m_timer.isRunning())
			m_timer.setRepeats(true);
		m_timer.start();
	}

	public void stopGenerating() {
		if (m_timer.isRunning())
			m_timer.stop();
	}

	public void setInterDelay(int second) {
		m_timer.setDelay(second * 1000);
	}

	public void changeMode(String modeName) {
		if (modeName.equals("Pure Random"))
			m_behavior = new PureRandom();
		else if (modeName.equals("Morning Peak"))
			m_behavior = new MorningPeak();
		else if(modeName.equals("Evening Peak"))
			m_behavior = new EveningPeak();
		else
			m_behavior = new PureRandom();
	}
	
	public static String[] getAllModeNames()
	{
		String[] result={"Pure Random","Morning Peak","Evening Peak"};
		return result;
	}

	/*
	 * timer listener
	 */

	private ActionListener timerListener = new ActionListener() {
		public void actionPerformed(ActionEvent arg0) {

			NewComingTask newTask = m_behavior.generateNewComingTask();

			m_leftTasks.addTask(newTask.getFloorAt(), newTask.getFloorTo());
			m_rightTaskQueueUtility.addToTaskQueue(newTask.getFloorAt(),
					newTask.getFloorTo());
		}
	};
}
